package _001_liang_shu_zhi_he;

import java.util.HashMap;
import java.util.Map;

/**
 * App
 *
 * @author yuand
 * @date 2021/8/24
 **/
public class App {

    public static void main(String[] args) {

        int[] ints = new Solution().twoSum(new int[]{2, 7, 11, 15}, 9);
        System.out.println(ints);
    }

    static class Solution {
        public int[] twoSum(int[] nums, int target) {
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = nums.length - 1; i >= 0; i--) {
                int value = nums[i];
                Integer idx = map.get(value);
                if (idx != null) {
                    return new int[]{i, idx};
                } else {
                    map.put(target - value, i);
                }
            }
            return null;
        }
    }

}
